/**
 * 
 */
package com.ocvdemo;

import java.io.File;

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.MatOfRect;
import org.opencv.core.Point;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
import org.opencv.objdetect.CascadeClassifier;

import com.imshow.JavaOpenCV.Imshow;

/**
 * @author Mishra
 *
 */
public final class TestUtils {
	
	public static Mat createGreyImage(Mat image) {
		return new Mat(image.size(), CvType.CV_8UC1);
	}
	
	public static MatOfRect detect(Mat inpt,String classifer,String imgDesc,Scalar color) {
		// Create a face detector from the cascade file in the resources
	    // directory.
	    CascadeClassifier faceDetector = new CascadeClassifier(getPath(classifer));
	    Mat image = Highgui.imread(getPath(imgDesc));
	    Mat gray = createGreyImage(image);
	    Imgproc.cvtColor(image, gray, Imgproc.COLOR_BGR2GRAY);
	    Imgproc.equalizeHist(gray, gray);
	    
	    
	    // Detect faces in the image.
	    // MatOfRect is a special container class for Rect.
	    MatOfRect detections = new MatOfRect();
	    faceDetector.detectMultiScale(gray, detections/*,1.1,2,0,new Size(0,0),new Size(20,20)*/);
	    
	 // Draw a bounding box around each face.
	    for (Rect rect : detections.toArray()) {
	        Core.rectangle(inpt, new Point(rect.x, rect.y), new Point(rect.x + rect.width, rect.y + rect.height), color);	        
	    }
	    
	    return detections;

	}
	
	public static Point centerOfRect(Rect rect) {
		Point pt = new Point();
		 pt.x = rect.x + rect.width/2;
		 pt.y = rect.y + rect.height/2;
		return pt;
	}
	
	public static void showImage(Mat img) {
		Imshow imshow = new Imshow("Image Viewer");
		imshow.showImage(img);
	}
	
	public static Mat readImage(String imgDesc) {
		String path = getPath(imgDesc);
		return Highgui.imread(path);				
	}

	public static String getPath(String imgDesc) {
		File path = new File("");
		StringBuilder sb = new StringBuilder();
		sb = sb.append(path.getAbsolutePath()).append(imgDesc);
//		System.out.println("Path "+sb.toString());
		return sb.toString();
	}


}
